<template>
  <div>
    <van-list v-model="loading" :finished="finished" finished-text="没有更多了" :error.sync="error" error-text="请求失败，点击重新加载"
      @load="onLoad">
      <van-cell v-for="item in list" :key="item.result" :title="item.title" />
    </van-list>
  </div>
</template>

<script>
import { getSearchResult } from '@/api/search.js'
export default {
  name: 'result',
  data () {
    return {
      list: [],
      loading: false, // 控制当前的加载状态
      finished: false, // 控制是否加载完成
      page: 1, // 页数
      per_page: 10, // 每页数量
      error: false
    }
  },
  props: {
    value: {
      type: String,
      required: true
    }
  },
  methods: {
    async onLoad () {
      try {
        //  1.获取数据
        const res = await getSearchResult({
          page: this.page,
          per_page: this.per_page,
          q: this.value
        })
        if (Math.random() > 0.5) {
          console.lo(123)
        }
        // 2.保存数据
        this.list.push(...res.data.data.results)
        // 3.当前这一次的加载状态结束
        this.loading = false
        // 4.数据全部加载完成
        if (this.list.length === res.data.data.total_count) {
          this.finished = true
        } else {
          this.page++
        }
      } catch (err) {
        this.loading = false
        this.error = true
      }
    }
  },
  computed: {},
  watch: {},
  filters: {},
  components: {},
  created () { },
  mounted () { }
}
</script>

<style scoped lang='less'>
</style>
